<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/5
 * Time: 10:12
 */
namespace app\api\controller;
use think\Model;
use think\Db;
use think\Request;
use app\common\model\HotelOrderModel as ho;
class Like extends Common
{
    /**
     * 收藏
     */
    public function collect()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $post['create_time'] = date('Y-m-d H:i:s');
            //查询是否已经收藏过
            if($post['type']==1){
                $is_collect = Db::name('collect')->where('uid',$post['uid'])->where('other_id',$post['other_id'])
                    ->where('type',5)->find();
            }else if($post['type']==2){
                $is_collect = Db::name('collect')->where('uid',$post['uid'])->where('other_id',$post['other_id'])
                    ->where('type',6)->find();
            }

            if($is_collect)
                $this->apiReturn('400','已收藏');
            $res = Db::name('collect')->insert($post);
            if($res){
                //修改基础参数
                if($post['type']==1){
                    $collect = Db::name('method')->where('mid',$post['other_id'])->find();
                    if($collect){
                        Db::name('method')->where('mid',$post['other_id'])->setInc('collect_num',1);
                        Db::name('method')->where('mid',$post['other_id'])->setInc('set_collect_num',mt_rand(1,5));
                    }
                }
                if($post['type']==2){
                    $collect = Db::name('friends_circle')->where('f_id',$post['other_id'])->find();
                    if($collect){
                        Db::name('friends_circle')->where('f_id',$post['other_id'])->setInc('collect_num',1);
                        Db::name('friends_circle')->where('f_id',$post['other_id'])->setInc('set_collect_num',mt_rand(1,5));
                    }
                }
                $this->apiReturn('200','收藏成功');
            }else{
                $this->apiReturn('400','收藏失败');
            }
        }
    }

    /*
     * 取消收藏
     */
    public function no_collect()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $res = Db::name('collect')->where('uid',$post['uid'])->where('other_id',$post['other_id'])
                ->where('type',$post['type'])->delete();
            if($res){
                if($post['type']==1){
                    Db::name('method')->where('mid',$post['other_id'])->setDec('collect_num',1);
                }
                if($post['type']==2){
                    Db::name('friends_circle')->where('f_id',$post['other_id'])->setDec('collect_num',1);
                }
                $this->apiReturn('200','取消收藏成功');
            }else{
                $this->apiReturn('400','取消收藏失败');
            }
        }
    }
}
